#ifndef THREADPOOL_H
#define THREADPOOL_H
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <errno.h>
#include <signal.h>
#include <crypt.h>
#include <openssl/md5.h>
#include "head.h"
#define Elemtype int
#define THREADNUM 3
#define TOKEN_SIZE 128
#define ARGS_CHECK(argc, num){if((argc) != (num)){fprintf(stderr,"args num error!\n");exit(-1);}}
#define ERROR_CHECK(ret, error_flag, msg){if((ret) == (error_flag)){perror(msg);exit(-1);}}
#define PTHREAD_ERROR_CHECK(ret, msg){if((ret) != 0){fprintf(stderr,"%s:%s\n",msg,strerror(ret));exit(-1);}}


typedef enum{
    //命令
    CD = 1,
    PWD,
    LS,
    MKDIR,
    RMDIR,
    PUTS,
    GETS,
    REMOVE,
    QUIT,
    ERROR_COMMAND,
    // 
    LOGIN,
    REGISTER,        // 新增注册命令
    LOGIN_SUCCESS,   // 登录成功响应
    LOGIN_FAIL,      // 登录失败响应
    REGISTER_SUCCESS,// 注册成功响应
    REGISTER_FAIL    // 注册失败响应
}cmdType;

typedef struct user_s{
    int netfd;
    char username[20];
    char userpwd[1024];
    struct user_s * next;
}user_t;

typedef struct userlist_s{
    user_t* head;
    user_t* tail;
    int userNum;
}userlist_t;

typedef struct train_s{
    cmdType command;
    int length;
    char token[TOKEN_SIZE];
    char data[1024];
}train_t;

typedef struct node_s{
    Elemtype netfd;
    struct node_s* next;
}node_t;

typedef struct taskQueue_s{
    node_t* front;
    node_t* rear;
    int queueSize;
}taskQueue_t;

typedef struct threadPool_s{
    //互斥锁
    pthread_mutex_t mutex;
    //条件变量
    pthread_cond_t cond;
    //共享数据
    taskQueue_t taskQueue;
    //线程信息
    pthread_t* childThreadSet;
    //线程数量
    int threadNum;
    //退出标志
    int exitFlag;
}threadPool_t;

//链表初始化
int linklist_init(userlist_t* userlist);
//插入链表
int linklist_insert(userlist_t* userlist, int netfd, char* username);
//删除链表节点
int linklist_delete(userlist_t* userlist, int netfd);
//队列初始化
int queue_init(taskQueue_t* queue);
//入队
int enqueue(taskQueue_t* queue, Elemtype data);
//出队
int dequeue(taskQueue_t* queue);
//线程池初始化
int thread_pool_init(threadPool_t* pool);
//创建工人线程
int make_worker(threadPool_t* pool);
//建立tcp
int tcp_init(char* ip, char* port);
//加入监听
int epoll_add(int epfd, int listenFd);
//删除监听
int epoll_del(int epfd, int listenFd);
//处理消息
int deal_message(int* netfd, threadPool_t* pool,char* user, DirStack* stack, MYSQL* conn);
//发送文件
int putsCommand(int sockfd,MYSQL* mysql,char* path, char* filename);
int sendn(int sockfd, void *buf, ssize_t length);
//接受文件
int getsCommand(int sockfd,char *cwd,char*filename);
int recvn(int sockfd, void *buf, ssize_t length);
//创建盐值
int salt(char* salts);
//注册登入
int login(int* netfd, MYSQL* db,char* userName, char* userPasswd);
#endif
